package com.wonders.bigdata.rpc.rpc_serial;

import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Description: Hessian序列化比JDK自带的序列化效率高，字节短<br>
 *
 * @author: XB
 * @date: 2021/4/25 11:00
 */
public class RPCHessianVSJDK {
    public static void main(String[] args) {
        try {
            Person person = new Person("小李", 25);
            System.out.println("========jdk序列化和反序列化========");
            byte[] jdkBytes = jdkSerializ(person);
            System.out.println("jdk序列化后长度=" + jdkBytes.length);
            Person jdkDesO = (Person)jdkDeserializ(jdkBytes);
            System.out.println("jdk反序列化后结果=" + jdkDesO.toString());
            System.out.println("========Hessian序列化和反序列化========");
            byte[] hessianBytes = hessianSerializ(person);
            System.out.println("Hessian序列化后长度=" + hessianBytes.length);
            Person hessianDesO = (Person)hessianDeserializ(hessianBytes);
            System.out.println("Hessian反序列化后结果=" + hessianDesO.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Hessian序列化方法
     * @param o
     * @return
     * @throws Exception
     */
    public static byte[] hessianSerializ(Object o) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Hessian2Output outputStream = new Hessian2Output(baos);
        outputStream.writeObject(o);
        outputStream.flush();
        byte[] bytes = baos.toByteArray();
        baos.close();
        outputStream.close();
        return bytes;
    }

    /**
     * Hessian反序列化方法
     * @param bytes
     * @return
     * @throws Exception
     */
    public static Object hessianDeserializ(byte[] bytes) throws Exception {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        Hessian2Input inputStream = new Hessian2Input(bais);
        Object o = inputStream.readObject();
        bais.close();
        inputStream.close();
        return o;
    }

    /**
     * jdk自带序列化方法
     * @param o
     * @return
     * @throws Exception
     */
    public static byte[] jdkSerializ(Object o) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream outputStream = new ObjectOutputStream(baos);
        outputStream.writeObject(o);
        outputStream.flush();
        byte[] bytes = baos.toByteArray();
        baos.close();
        outputStream.close();
        return bytes;
    }

    /**
     * jdk自带反序列化方法
     * @param bytes
     * @return
     * @throws Exception
     */
    public static Object jdkDeserializ(byte[] bytes) throws Exception {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        ObjectInputStream inputStream = new ObjectInputStream(bais);
        Object o = inputStream.readObject();
        bais.close();
        inputStream.close();
        return o;
    }
}
